home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / packmail.py < prev    next >
Text File  |  2005-11-19  |  3KB  |  112 lines

  1. # Module 'packmail' -- create a self-unpacking shell archive.
  2.  
  3. # This module works on UNIX and on the Mac; the archives can unpack
  4. # themselves only on UNIX.
  5.  
  6. import os
  7. from stat import ST_MTIME
  8.  
  9. # Print help
  10. def help():
  11.     print 'All fns have a file open for writing as first parameter'
  12.     print 'pack(f, fullname, name): pack fullname as name'
  13.     print 'packsome(f, directory, namelist): selected files from directory'
  14.     print 'packall(f, directory): pack all files from directory'
  15.     print 'packnotolder(f, directory, name): pack all files from directory'
  16.     print '                        that are not older than a file there'
  17.     print 'packtree(f, directory): pack entire directory tree'
  18.  
  19. # Pack one file
  20. def pack(outfp, file, name):
  21.     fp = open(file, 'r')
  22.     outfp.write('echo ' + name + '\n')
  23.     outfp.write('sed "s/^X//" >"' + name + '" <<"!"\n')
  24.     while 1:
  25.         line = fp.readline()
  26.         if not line: break
  27.         if line[-1:] != '\n':
  28.             line = line + '\n'
  29.         outfp.write('X' + line)
  30.     outfp.write('!\n')
  31.     fp.close()
  32.  
  33. # Pack some files from a directory
  34. def packsome(outfp, dirname, names):
  35.     for name in names:
  36.         print name
  37.         file = os.path.join(dirname, name)
  38.         pack(outfp, file, name)
  39.  
  40. # Pack all files from a directory
  41. def packall(outfp, dirname):
  42.     names = os.listdir(dirname)
  43.     try:
  44.         names.remove('.')
  45.     except:
  46.         pass
  47.     try:
  48.         names.remove('..')
  49.     except:
  50.         pass
  51.     names.sort()
  52.     packsome(outfp, dirname, names)
  53.  
  54. # Pack all files from a directory that are not older than a give one
  55. def packnotolder(outfp, dirname, oldest):
  56.     names = os.listdir(dirname)
  57.     try:
  58.         names.remove('.')
  59.     except:
  60.         pass
  61.     try:
  62.         names.remove('..')
  63.     except:
  64.         pass
  65.     oldest = os.path.join(dirname, oldest)
  66.     st = os.stat(oldest)
  67.     mtime = st[ST_MTIME]
  68.     todo = []
  69.     for name in names:
  70.         print name, '...',
  71.         st = os.stat(os.path.join(dirname, name))
  72.         if st[ST_MTIME] >= mtime:
  73.             print 'Yes.'
  74.             todo.append(name)
  75.         else:
  76.             print 'No.'
  77.     todo.sort()
  78.     packsome(outfp, dirname, todo)
  79.  
  80. # Pack a whole tree (no exceptions)
  81. def packtree(outfp, dirname):
  82.     print 'packtree', dirname
  83.     outfp.write('mkdir ' + unixfix(dirname) + '\n')
  84.     names = os.listdir(dirname)
  85.     try:
  86.         names.remove('.')
  87.     except:
  88.         pass
  89.     try:
  90.         names.remove('..')
  91.     except:
  92.         pass
  93.     subdirs = []
  94.     for name in names:
  95.         fullname = os.path.join(dirname, name)
  96.         if os.path.isdir(fullname):
  97.             subdirs.append(fullname)
  98.         else:
  99.             print 'pack', fullname
  100.             pack(outfp, fullname, unixfix(fullname))
  101.     for subdirname in subdirs:
  102.         packtree(outfp, subdirname)
  103.  
  104. def unixfix(name):
  105.     comps = name.split(os.sep)
  106.     res = ''
  107.     for comp in comps:
  108.         if comp:
  109.             if res: res = res + '/'
  110.             res = res + comp
  111.     return res
  112.